﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using RS;

namespace RSAgricutureDroughtWaterloggingApp
{
    public partial class DroughtWater : Form
    {
        public DroughtWater()
        {
            InitializeComponent();
        }

        private void mButtonSkimInputImg1_Click(object sender, EventArgs e)
        {
            //定义打开文件对话框
            OpenFileDialog dlg = new OpenFileDialog();

            //设置对话框属性
            dlg.Filter = "tif jpg(*.tif *.jpg)|*.tif;*.jpg|tif(*.tif)|*.tif|All files (*.*)|*.*";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;

            //显示对话框
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                mTextBoxInputImg1.Text = dlg.FileName;
            }
        }

        private void mButtonSkimInputImg2_Click(object sender, EventArgs e)
        {
            //定义打开文件对话框
            OpenFileDialog dlg = new OpenFileDialog();

            //设置对话框属性
            dlg.Filter = "tif jpg(*.tif *.jpg)|*.tif;*.jpg|tif(*.tif)|*.tif|All files (*.*)|*.*";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;

            //显示对话框
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                mTextBoxInputImg2.Text = dlg.FileName;
            }
        }

        private void mButtonStart_Click(object sender, EventArgs e)
        {
            //检测输入文件路径是否为空
            if ("" == mTextBoxInputImg1.Text || "" == mTextBoxInputImg2.Text )
            {
                label4.Text = "Input Image is Null";
                return;
            }

            //检测输入文件是否存在
            if (!File.Exists(mTextBoxInputImg1.Text))
            {
                label4.Text = "Input Image1 is not exist";
                return;
            }
            if (!File.Exists(mTextBoxInputImg2.Text))
            {
                label4.Text = "Input Image2 is not exist";
                return;
            }

            //检测输出文件是否是文件夹
            FileInfo fileInfo = new FileInfo(mTextBoxOutputImg.Text);
            if ( fileInfo.Attributes == FileAttributes.Directory )
            {
                label4.Text = "Output Image String is Directory";
                return;
            }

            disPose();

//            setControlsEnable(false);
//             Thread t = new Thread(new ThreadStart(disPose));
//             t.Start();
//            t.Join();
//            setControlsEnable(true);
        }

        private void mButtonOutImgSkim_Click(object sender, EventArgs e)
        {
            //定义保存文件对话框
            SaveFileDialog dlg = new SaveFileDialog();

            //设置保存文件对话框属性
            dlg.Filter = "jpg(*.jpg)|*.jpg|tif(*.tif)|*.tif|All files (*.*)|*.*";
            dlg.FilterIndex = 2;

            //显示对话框
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                mTextBoxOutputImg.Text = dlg.FileName;
            }
        }

        private void setControlsEnable(bool isEnable)
        {
            mTextBoxInputImg1.Enabled = isEnable;
            mTextBoxInputImg2.Enabled = isEnable;
            mTextBoxOutputImg.Enabled = isEnable;

            mButtonSkimInputImg1.Enabled = isEnable;
            mButtonSkimInputImg2.Enabled = isEnable;
            mButtonOutImgSkim.Enabled = isEnable;

            mButtonStart.Enabled = isEnable;
        }

        private void disPose()
        {
//            label4.Text = "disPose";
            //定义算法
            RSAgricutureDroughtWaterloggingWrapper rs = new RSAgricutureDroughtWaterloggingWrapper();
            
            //设置算法参数
            if (!rs.setNewInputImg(mTextBoxInputImg2.Text))
            {
                label4.Text = rs.getMsg();
                return;
            }
            if (!rs.setInputImg(mTextBoxInputImg2.Text))
            {
                label4.Text = rs.getMsg();
                return;
            }
            rs.setFlag(1);
            rs.setOutputImgFormat(mTextBoxOutputImg.Text);
            rs.setFunctionCallBack(showProgressText);
            
            //启动算法
            if (!rs.disPose())
            {
                label4.Text = "";
                return;
            }

            

//             for (int i = 0; i < 1000; i++)
//             {
//                 mProgressBar.Value = i;
//                 Thread.Sleep(10);
//             }

//            mProgressBar.Value = 1000;

            label4.Text = "end";

            double ndvi = rs.getNDVI();
            label4.Text = ndvi.ToString();
        }

        private void showProgressText(int nPos, String text )
        {
//            mProgressBar.Value = nPos;
 //           label4.Text = text;
        }
    }
}
